﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>
@{
    
 var model = ViewBag.Model;

    var jsonStr = new HtmlString(new JavaScriptSerializer().Serialize(model));
}

@Html.Partial("_ProductVariantInfo_CreateOrEdit")

<input type="button" value="Create" data-bind="click:SaveProductVariantInfo"/>

   
@Styles.Render("~/content/fineuploader/fineuploader-3.5.0.css")
    @Scripts.Render("~/scripts/jquery-1.7.1.js")
    @Scripts.Render("~/content/fineuploader/fineuploader-3.5.0.js")
    @Scripts.Render("~/content/fineuploader/iframe.xss.response-3.5.0.js")

@section scripts{

    
<script>
    var init = @jsonStr;


    function ProductVariantModel() {
        var self = this;
        self.Id = ko.observable(init.Id);
        self.Name = ko.observable(init.Name);
        self.Price= ko.observable(init.Price);
        self.Sku = ko.observable(init.Sku);
        self.DisplayOrder= ko.observable(init.DisplayOrder);
        self.StockQuantity = ko.observable(init.StockQuantity);
        self.PictureId = ko.observable(init.PictureId);
        self.PictureUrl= ko.observable(init.PictureUrl);
        self.Published = ko.observable(init.Published);
        self.ProductId = ko.observable(init.ProductId);
        
        self.SaveProductVariantInfo = function() {
            $.ajax({
                url: "/api/ProductVariantApi/InsertProductVariantInfo",
                type: 'post',
                data: ko.toJSON(self),
                contentType: 'application/json',
                success: function (result) {

                    if (result.Status == "0") {
                        alert("保存成功");
                        location.href = '/Product/Edit?productId=' + self.ProductId();
                    }
                    else {
                        alert("保存失败：" + result.Msg);
                    }
                }
            });
        };

    }

    var vm = new ProductVariantModel();
    ko.applyBindings(vm);
    

 


    $(document).ready(function() {

        var errorHandler = function(event, id, fileName, reason) {
            qq.log("id: " + id + ", fileName: " + fileName + ", reason: " + reason);
        };

        
        var uploader2 = new qq.FineUploader({
            element: $('#manualUploadModeExample')[0],
            autoUpload: true,
            uploadButtonText: "Select Files",
            request: {
                endpoint: "/FineUploader/UploadFile",
                params: { "extraParam1": "true", "extraParam2": "212" }                
            },
            callbacks: {
                onError: errorHandler,
                onComplete: function (id, fileName, responseJSON) {
                    if (responseJSON.success) {
                        vm.PictureId(responseJSON.PictureId);
                        vm.PictureUrl(responseJSON.PictureUrl);
                    }
                }
            }
        });
    });
</script>
}